package com.wemei.auto

import android.content.Context
import android.graphics.drawable.Drawable
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.TextView
import androidx.core.content.ContextCompat

open class ClearTextView : TextView, TextWatcher {
    constructor(context: Context) : this(context, null)
    constructor (context: Context, attrs: AttributeSet?) : this(context, attrs, android.R.attr.editTextStyle)
    constructor (context: Context, attrs: AttributeSet?, defStyle: Int = android.R.attr.editTextStyle) : super(
        context,
        attrs,
        defStyle
    ) {
        init(attrs)
    }
    /**
     * 删除按钮的引用
     */
    private var mClearDrawable: Drawable? = null
    @JvmField
    var clearListener: (() -> Unit)? = null
    var showClear: Boolean = true
    private fun init(attrs: AttributeSet?) {
        //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
        mClearDrawable = compoundDrawables[2]
        if (mClearDrawable == null) {
//          throw new NullPointerException("You can add drawableRight attribute in XML");
            if (attrs != null) {
                val a = context.obtainStyledAttributes(attrs, R.styleable.InputEditText)
                mClearDrawable = a.getDrawable(R.styleable.InputEditText_ie_clear)
                a.recycle()
            }
            if (mClearDrawable == null) mClearDrawable = ContextCompat.getDrawable(context, R.drawable.delete_selector)
        }
        mClearDrawable!!.setBounds(0, 0, mClearDrawable!!.intrinsicWidth, mClearDrawable!!.intrinsicHeight)
        //默认设置隐藏图标
        setClearIconVisible(false)
        isFocusableInTouchMode = false
        //设置输入框里面内容发生改变的监听
        addTextChangedListener(this)
        compoundDrawablePadding = 40
    }
    /**
     * 因为我们不能直接给EditText设置点击事件，所以我们用记住我们按下的位置来模拟点击事件
     * 当我们按下的位置 在  EditText的宽度 - 图标到控件右边的间距 - 图标的宽度  和
     * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标，竖直方向就没有考虑
     */
    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP && showClear) {
            if (compoundDrawables[2] != null) {
                val touchable = event.x > width - totalPaddingRight && event.x < width - paddingRight
                if (touchable) {
                    this.text = ""
                    clearListener?.invoke()
                    return true
                }
            }
        }
        return super.onTouchEvent(event)
    }
    /**
     * 设置清除图标的显示与隐藏，调用setCompoundDrawables为EditText绘制上去
     *
     * @param visible
     */
    private fun setClearIconVisible(visible: Boolean) {
        if (showClear) {
            val right = if (visible) mClearDrawable else null
            setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], right, compoundDrawables[3])
        }
    }
    /**
     * 当输入框里面内容发生变化的时候回调的方法
     */
    override fun onTextChanged(
        s: CharSequence, start: Int, count: Int, after: Int,
    ) {
        setClearIconVisible(s.isNotEmpty())
    }

    override fun beforeTextChanged(
        s: CharSequence, start: Int, count: Int, after: Int,
    ) {
    }

    override fun afterTextChanged(s: Editable) {}
}